const enterpriseVue = new Vue({
    el: '#app',
    data() {
        return {
            rules: {

            },
            form: {
                Page_SerialNumber: '',
                Page_Group_SerialNumber: '',
                Page_Type_SerialNumber: '',
                Page_Name: '',
                Page_JSON: '',
                Start_Date: '',
                End_Date: ''
            },
            Type: [],
            Group: [],
            loading: false,
        }
    },
    mounted() {
        this.form.Page_Group_SerialNumber = sessionStorage.getItem("Page_Group_SerialNumber");
        const Page_SerialNumber = sessionStorage.getItem("Page_SerialNumber");
        this.getGroup(Page_SerialNumber);
        this.getData(this.form.Page_Group_SerialNumber);
    },
    methods: {
        getData(Page_Group_SerialNumber) {
            const _this = this
            $.ajax({
                url: DomainName + '/getInquiryIDAction?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Data: 'page_type',
                    Primary_Key: Page_Group_SerialNumber,
                    Primary_Key_Name: 'Page_Group_SerialNumber',
                    isNullEnterprise: true
                },
                success: function (res) {
                    _this.Type = res
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        getGroup(Primary_Key) {
            const _this = this;
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Data: 'page_group',
                },
                success: function (res) {
                    _this.Group = res
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
            $.ajax({
                url: DomainName + '/getInquiryIDOriginal?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Data: 'page_info',
                    Primary_Key,
                    Primary_Key_Name: 'Page_SerialNumber',
                    isNullEnterprise: true
                },
                success: function (res) {
                    _this.form = res[0]
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        handleChange(e) {
            console.log(e);
            this.form.Page_Type_SerialNumber = ''
            this.Type = []
            this.getData(e)
        },
        submitForm(formName) {
            let _this = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true
                    if (_this.form.Parent_Page) {
                        _this.form.Leven = parseInt(_this.Parent.filter(i => i.Page_SerialNumber == _this.form.Parent_Page)[0].Leven) + 1
                    } else {
                        _this.form.Leven = 1
                    }
                    if(this.form.End_Date == null){
                        this.form.End_Date = ''
                    }
                    const { add, modify } = _this.deal()
                    let data = {}
                    if (add == null) {
                        data = { 
                            Data: JSON.stringify({ Modify: { page_info: [modify] } }) ,
                            Operation_Name: '修改页面'
                        }
                    } else {
                        data = { 
                            Data: JSON.stringify({ Modify: { page_info: [modify] }, Add: { page_info: [add] }}),
                            Operation_Name: '修改页面'
                        }
                    }
                    $.ajax({
                        url: `${DomainName}/getEditAction?token=${$.cookie('token')}`,
                        type: 'POST',
                        data,
                        success: function (res) {
                            if (res.Status === 'success') {
                                _this.$message.success("提交成功")
                                window.history.go(-1);
                            } else {
                                _this.$message.error("提交失败，请稍后再试")
                            }
                        },
                        error: function () {
                            _this.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            _this.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        deal() {
            const modify = deepClone(this.form)
            return { add: null, modify };
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})